import React from 'react';
import ReactDOM from 'react-dom';
import { HashRouter as Router, Route, Switch, Redirect } from 'react-router-dom'
import { createHashHistory as createHistory } from "history";
import { Provider } from 'mobx-react';
import { asyncComponent } from 'react-async-component';
import { Tools } from 'weaver-mobile';
import { FloatingTouch } from 'weaver-mobile-page';
import allStore from './stores';
import './style';

const coms = {
  Home: asyncComponent({
    resolve: () => import("./components/main")
  }),
  Enter: asyncComponent({
    resolve: () => import("./components/main/Enter")
  }),
  CustomerAddExist: asyncComponent({
    resolve: () => import("./components/form/CustomerAddExist")
  }),
  AuthorityAdd: asyncComponent({
    resolve: () => import("./components/form/AuthorityAdd")
  }),
  BusinessCard: asyncComponent({
    resolve: () => import("./components/form/BusinessCard")
  }),
  CustomerCard: asyncComponent({
    resolve: () => import("./components/card/Customer")
  }),
  ChanceCard: asyncComponent({
    resolve: () => import("./components/card/Chance")
  }),
  ContacterCard: asyncComponent({
    resolve: () => import("./components/card/Contacter")
  }),
  ContacterForm: asyncComponent({
    resolve: () => import("./components/form/ContacterForm")
  }),
  CustomerMap: asyncComponent({
    resolve: () => import("./components/card/CustomerMap")
  }),
  CustomerVisit: asyncComponent({
    resolve: () => import("./components/card/CustomerVisit")
  }),
  CustomerShareList: asyncComponent({
    resolve: () => import("./components/list/CustomerShareList")
  }),
  ContactList: asyncComponent({
    resolve: () => import("./components/list/ContactList")
  }),
  CommerceCard: asyncComponent({
    resolve: () => import("./components/card/Commerce")
  }),
  StickyBar: asyncComponent({
    resolve: () => import("./components/public/StickyBar")
  }),
  HrmCustomerList: asyncComponent({
    resolve: () => import("./components/list/HrmCustomerList")
  }),
  Commerce: asyncComponent({
    resolve: () => import("./components/card/Commerce/Commerce")
  }),
  HomePage: asyncComponent({
    resolve: () => import("./components/main/HomePage")
  }),
  ContactHomePage: asyncComponent({
    resolve: () => import("./components/list/ContactHomePage")
  }),
  SellsReport: asyncComponent({
    resolve: () => import("./components/report/SellsReport")
  }),
  ContactRemindList: asyncComponent({
    resolve: () => import("./components/list/ContactRemindList")
  }),
  CrmList: asyncComponent({
    resolve: () => import("./components/list/CrmList")
  }),
  Remind: asyncComponent({
    resolve: () => import("./components/remind")
  }),
}

class Root extends React.Component {

  componentWillMount() {
    window.useFloatingTouch = true;
  }

  onHomePageClick() {
    let objs = document.getElementsByClassName("wm-route-layout");
    const pageCount = objs.length;
    window.history.go(-pageCount);
    setTimeout(() => {
      for (let obj of objs) {
        obj.style.display = "none";
      }
    }, 300);
  }

  render() {
    return (
      <Provider {...allStore}>
        <Router>
          <FloatingTouch onClick={() => this.onHomePageClick()}>
            <Route name="home" path="/" render={() => {
              return (
                <coms.Home>
                  <div style={{ height: "100%" }}>
                    <Switch>
                      {/* <Route name="main" path="/main/:type/:listTabIndex" component={coms.Enter} /> */}
                      <Route name="customerAddExist" path="/customerAddExist" component={coms.CustomerAddExist} />
                      <Route name="authorityAdd" path="/authorityAdd" component={coms.AuthorityAdd} />
                      <Route name="businessCard" path="/BusinessCard" component={coms.BusinessCard} />
                      <Route name="contacterAdd" path="/contacterAdd/:operation" component={coms.ContacterForm} />
                      <Route name="customerCard" path="/customerCard/:id" component={coms.CustomerCard} />
                      <Route name="chanceCard" path="/chanceCard/:id" component={coms.ChanceCard} />
                      <Route name="contacterCard" path="/contacterCard/:id" component={coms.ContacterCard} />
                      <Route name="contacterAdd" path="/contacterForm/:customerId/:operation/:rpath" component={coms.ContacterForm} />
                      <Route name="contacterEdit" path="/contacterForm/:contacterId/:operation" component={coms.ContacterForm} />
                      <Route name="customerMap" path="/customerMap/:customerId" component={coms.CustomerMap} />
                      <Route name="customerVisit" path="/customerVisit/:customerId" component={coms.CustomerVisit} />
                      <Route name="contactList" path="/contactList/:customerId/:chanceId" component={coms.ContactList} />
                      <Route name="customerShareList" path="/customerShareList/:id" component={coms.CustomerShareList} />
                      <Route name="commerceCard" path="/CommerceCard/:id" component={coms.CommerceCard} />
                      <Route name="commerce" path="/commerce/:name" component={coms.Commerce} />
                      <Route name="stickyBar" path="/stickyBar" component={coms.StickyBar} />
                      <Route name="hrmCustomerList" path="/hrmCustomerList/:manager" component={coms.HrmCustomerList} />
                      <Route name="sellsReport" path="/sellsReport" component={coms.SellsReport} />
                      <Route name="contactHomePage" path="/contactHomePage/:subType" component={coms.ContactHomePage} />
                      <Route name="contactRemindList" path="/contactRemindList" component={coms.ContactRemindList} />
                      <Route name="list" path="/list/:type/:listTabIndex" component={coms.CrmList} />
                      <Route name="enter" path="/enter" component={coms.HomePage} />
                      <Route name="remind" path="/remind" component={coms.Remind} />
                      {/* <Redirect exact path="/enter" to={{pathname: '/homePage'}} /> */}
                    </Switch>
                    <iframe id="crm_contact_attach" frameBorder="0" style={{ display: "none" }}></iframe>
                  </div>
                </coms.Home>
              )
            }} />
          </FloatingTouch>
        </Router>
      </Provider>
    )
  }
}

Tools.getLocaleLabel("crm", () => { // 多语言
  ReactDOM.render(<Root />, document.getElementById('root'));
});